<template>
	<view class="page" :style="'background-image: url('+indexBg+');'">
    <u-navbar :fixed="false" title="帐号管理" placeholder bgColor="transparent"></u-navbar>
    <view class="form-info">
      <view class="form-box u-m-b-48">
        <view class="form-item">
          <view class="form-lable required">手机号</view>
          <view class="form-content">
            <u--input tye="number" placeholder="请输入" :maxlength="11" border="none" inputAlign="right" v-model="form.phone" />
          </view>
        </view>
        <view class="form-item">
          <view class="form-lable required">姓名</view>
          <view class="form-content">
            <u--input placeholder="请输入" border="none" inputAlign="right" v-model="form.userName" />
          </view>
        </view>
        <view class="form-item" v-if="!form.id">
          <view class="form-lable required">登录密码</view>
          <view class="form-content">
            <u--input placeholder="请输入" border="none" inputAlign="right" v-model="form.password" />
            <view class="form-content-tips">电脑端登录使用</view>
          </view>
        </view>
        <view class="form-item">
          <view class="form-lable required">选择角色</view>
          <view class="form-content-right">
            <u-checkbox-group v-model="form.role" placement="column"
              activeColor="#fff" iconColor="#2b8e88">
              <u-checkbox :customStyle="{marginBottom: '16rpx'}" v-for="item in roleList" :key="item.id" :label="item.roleName" :name="item.id" />
            </u-checkbox-group>
          </view>
        </view>
      </view>
      <view class="save-primary-btn" @click="submit">保存提交</view>
    </view>
  </view>
</template>

<script>
import { getPlatform, addPlatform, updatePlatform, platformRoleList } from '@/api/index'
import { phoneReg } from '@/common/util'

export default {
  data() {
    return {
      indexBg: 'https://gowork365.com/prod-api/profile/mini/index/indexBg1.png',
      id: '',
      form: {
        phone: '',
        userName: '',
        password: '',
      },
      defaultForm: {
        phone: '',
        userName: '',
        password: '',
      },
      roleList: []
    }
  },
  onLoad(options) {
    this.getRoleList()
    if(options.id) {
      this.id = options.id
      this.getForm()
      delete this.defaultForm.password
    }
  },
  methods: {
    getRoleList() {
      platformRoleList({}).then(response => {
        this.roleList = response.data
      });
    },
    getForm() {
      uni.showLoading({ mask: true })
      getPlatform(this.id).then(response => {
        uni.hideLoading()
        if(response.data.role) {
          response.data.role = response.data.role.split(',')
        }
        if(response.data.roleNames) {
          response.data.roleNames = response.data.roleNames.split(',')
        }
        this.form = response.data;
      });
    },
    submit() {
      const that = this
      let formFlag = false
      for(let key in this.defaultForm) {
        if(this.form[key] === '') {
          formFlag = true
        }
      }
      if(formFlag) {
        uni.showToast({
          icon: 'none',
          title: '请完善基本信息'
        })
        return
      }
      if(!phoneReg(this.form.phone)) {
        return
      }
      let role = ''
      let roleNames = ''
      if(this.form.role) {
        role = this.form.role.join(',')
        const list = this.roleList.filter(v => this.form.role.findIndex(j => j == v.id) != -1) 
        roleNames = list.map(v => v.roleName).join(',')
      }
      uni.showLoading({ mask: true })
      if (this.form.id != null) {
        updatePlatform({...this.form, role, roleNames}).then(response => {
          uni.hideLoading()
          uni.showToast({
            icon: 'none',
            title: '修改成功'
          })
          setTimeout(() => {
            that.goBack(1)
          }, 1000)
        });
      } else {
        addPlatform({...this.form, role, roleNames}).then(response => {
          uni.hideLoading()
          uni.showToast({
            icon: 'none',
            title: '新增成功'
          })
          setTimeout(() => {
            that.goBack(1)
          }, 1000)
        });
      }
    }
  }
}
</script>

<style lang="less" scoped>
.page {
  width: 100%;
  min-height: 100vh;
  background: #F4F7F6;
  position: relative;
  background-repeat: no-repeat;
  background-size: 750rpx 690rpx;
  padding-bottom: 188rpx;
  display: flex;
  flex-direction: column;
  .form-info {
    flex: 1;
  }
}
</style>
